﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

[Serializable]
public class Suit
{
    [Serializable]
    public class RandProperty
    {
        public int isRand = 0;
        public List<PropertyValue<object>> value;
    }
    //套装属性
    public string id = "";
    public string name = "";
    private int levelUpLimit = 5;//等级上限
    public List<SkillBase> numProperty = new List<SkillBase>();
    public List<string> weaponList = new List<string>();
    public RandProperty randProperty = new RandProperty();

    public Suit()
    {
        ResetSkill();
    }
    /// <summary>
    /// 获得随机加成的属性
    /// num 0-4 表示2-6件
    /// </summary>
    /// <returns></returns>
    public PropertyValue<object> GetRandProperty(int num)
    {
        if(randProperty.value != null)
        {
            if(num < 0 || num >= randProperty.value.Count) return null;
            return randProperty.value[num];
        }
        return null;
    }
    /// <summary>
    /// 激活随机属性
    /// </summary>
    public void ActiveRandProperty()
    {
        //确定随机属性
        for(int k = 0;k < numProperty.Count;k++){
            var hasPropertyList = GetHasPropertyList(k);
            for (int j = 0; j < hasPropertyList.Count; j++)
            {
                hasPropertyList[j].val = Utils.GetRandVal(hasPropertyList[j].val.ToString());
                Debug.Log(hasPropertyList[j].GetDiscript() + "   " + hasPropertyList[j].GetVal()[0].ToString());
            }
        }
        ActiveSuitRandProperty();
    }
    /// <summary>
    /// 激活套装随机属性
    /// </summary>
    public void ActiveSuitRandProperty()
    {
        if (randProperty.isRand == 1) {
            randProperty.value = SuitRand.suitRandProperty.GetRandOne();
            //确定随机属性
            for(int k = 0;k < randProperty.value.Count;k++){
                if(randProperty.value[k] == null) continue;
                randProperty.value[k].val = Utils.GetRandVal(randProperty.value[k].val.ToString());
                Debug.Log(randProperty.value[k].GetDiscript() + "   " + randProperty.value[k].GetVal()[0].ToString());
            }
        }
    }
    /// <summary>
    /// 重置随机属性
    /// </summary>
    private void ResetRandProperties()
    {
        randProperty.isRand = 0;
        randProperty.value = null;
    }
    /// <summary>
    /// 获得套装名称
    /// </summary>
    /// <returns></returns>
    public string GetSuitName()
    {
        return LTLocalization.GetText(name);
    }
    /// <summary>
    /// 重置技能
    /// </summary>
    private void ResetSkill()
    {
        // Debug.Log("ResetSkill");
        numProperty = new List<SkillBase>();
        for (int i = 0; i < levelUpLimit; i++)
        {
            numProperty.Add(new SuitNum());
        }
    }
    /// <summary>
    /// 加入套装
    /// </summary>
    /// <param name="weaponId"></param>
    public void AddWeapon(string weaponId){
        if(weaponList.Contains(weaponId)) return;
        weaponList.Add(weaponId);
    }
    /// <summary>
    /// 从套装中移除
    /// </summary>
    /// <param name="weaponId"></param>
    public void RemoveWeapon(string weaponId){
        if(!weaponList.Contains(weaponId)) return;
        weaponList.Remove(weaponId);
    }
    /// <summary>
    /// 获得所有属性
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public List<PropertyValue<object>> GetHasPropertyList(int level)
    {
        var hasPropertyList = new List<PropertyValue<object>>();
        if (numProperty[level] == null) return hasPropertyList;
        var levelSkill = numProperty[level];
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                PropertyValue<object> pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0") hasPropertyList.Add(pro);
            }
        }
        return hasPropertyList;
    }
    /// <summary>
    /// 获得属性描述
    /// </summary>
    /// <param name="level"></param>
    /// <returns></returns>
    public string GetPropertyDiscript(int level)
    {
        if (numProperty[level] == null) return "";
        var levelSkill = numProperty[level];
        string discript = "";
        bool isAdd = false;
        //属性大类
        for (int i = 0; i < levelSkill.propertyList.Count; i++)
        {
            isAdd = false;
            //属性小类
            for (int j = 0; j < levelSkill.propertyList[i].val.propertyList.Count; j++)
            {
                var pro = levelSkill.propertyList[i].val.propertyList[j];
                //不为0说明具备
                if (pro.val.ToString() != "0")
                {
                    //大类描述
                    // if (!isAdd)
                    // {
                    //     isAdd = true;
                    //     if (discript != "") discript += ",";
                    //     discript += levelSkill.propertyList[i].GetDiscript() + ":";
                    // }
                    // else
                    // {
                        if (discript != "") discript += ",";
                    // }
                    discript += pro.GetDiscript();
                }
            }
        }
        //随机属性
        if(level % 2 == 0 && randProperty.isRand == 1)  {
            if(discript != "") discript += ",";
            discript += "随机属性";
        }
        discript = (level + 2) + "件套: " + discript;
        return discript;
    }
    public void InitData(){
        // foreach(var item in numProperty){
        //     item.InitData();
        // }
    }
    /// <summary>
    /// 深拷贝
    /// 修改对象内容不会互相影响
    /// </summary>
    /// <returns></returns>
    public Suit DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream,this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as Suit;
        }
    }
}

